package com.lujianfei.waterpower.utils.CameraAlbum;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;

public class ImagePicker {

    public static final int REQUEST_CODE_CAMERA = 100;
    public static final int REQUEST_CODE_ALBUM = 101;
    public static final int REQUEST_CODE_CROP = 102;

    public void openCamera(Activity activity,File outputFile){
        Intent intent = new Intent();
        intent.setAction("android.media.action.IMAGE_CAPTURE");
        intent.addCategory("android.intent.category.DEFAULT");
        Uri uri = FileProviderUtils.uriFromFile(activity, outputFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        activity.startActivityForResult(intent, REQUEST_CODE_CAMERA);
    }

    public void openAlbum(Activity activity){
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction("android.intent.action.PICK");
        intent.addCategory("android.intent.category.DEFAULT");
        activity.startActivityForResult(intent, REQUEST_CODE_ALBUM);
    }

    public void crop(Activity activity, Uri uri, File outputFile) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        FileProviderUtils.setIntentDataAndType(activity, intent, "image/*", uri, true);
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        //return-data为true时，直接返回bitmap，可能会很占内存，不建议，小米等个别机型会出异常！！！
        //所以适配小米等个别机型，裁切后的图片，不能直接使用data返回，应使用uri指向
        //裁切后保存的URI，不属于我们向外共享的，所以可以使用fill://类型的URI
        Uri outputUri = Uri.fromFile(outputFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
        intent.putExtra("return-data", false);

        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        activity.startActivityForResult(intent, REQUEST_CODE_CROP);
    }

    public static class Builder{

        private ImagePicker imagePicker;
        private Activity activity;
        private File outputFile;
        private Uri uri;
        public Builder(Activity activity){
            this.activity = activity;
            imagePicker = new ImagePicker();
        }
        public Builder setOutputFile(File outputFile){
            this.outputFile = outputFile;
            return this;
        }
        public Builder setUri(Uri uri){
            this.uri = uri;
            return this;
        }
        public void openCamera(){
            imagePicker.openCamera(activity,outputFile);
        }

        public void openAlbum(){
            imagePicker.openAlbum(activity);
        }

        public void openCrop(){
            imagePicker.crop(activity,uri,outputFile);
        }
    }
}
